package com.lpwa.web.controller.system;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.lpwa.common.annotation.Log;
import com.lpwa.common.core.controller.BaseController;
import com.lpwa.common.core.domain.AjaxResult;
import com.lpwa.common.core.page.TableDataInfo;
import com.lpwa.common.enums.BusinessType;
import com.lpwa.common.utils.RedisUtil;
import com.lpwa.common.utils.StaticRedisUtil;
import com.lpwa.common.utils.StringUtils;
import com.lpwa.common.utils.poi.ExcelUtil;
import com.lpwa.framework.netty.entity.LocatorMacCtx;
import com.lpwa.system.domain.*;
import com.lpwa.system.service.ILableService;
import com.lpwa.system.service.ISysEsmapService;
import com.lpwa.system.service.ISysLocatorService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * 基站管理Controller
 * 
 * @author ruoyi
 * @date 2020-11-17
 */
@Controller
@RequestMapping("/system/locator")
public class SysLocatorController extends BaseController
{
    private String prefix = "system/locator";
    @Autowired
    private ISysEsmapService sysEsmapService;
    @Autowired
    private ISysLocatorService sysLocatorService;
    @Autowired
    private ILableService lableService;
    @Autowired
    private RedisUtil redisUtil;
    @RequiresPermissions("system:locator:view")
    @GetMapping()
    public String locator()
    {
        return prefix + "/locator";
    }

    /**
     * 查询基站管理列表
     */
    @RequiresPermissions("system:locator:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(SysLocator sysLocator)
    {
        startPage();
        List<SysLocator> list = sysLocatorService.selectSysLocatorList(sysLocator);
        return getDataTable(list);
    }

    /**
     * 导出基站管理列表
     */
    @RequiresPermissions("system:locator:export")
    @Log(title = "基站管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(SysLocator sysLocator)
    {
        List<SysLocator> list = sysLocatorService.selectSysLocatorList(sysLocator);
        ExcelUtil<SysLocator> util = new ExcelUtil<SysLocator>(SysLocator.class);
        return util.exportExcel(list, "locator");
    }

    /**
     * 新增基站管理
     */
    @GetMapping("/add")
    public String add(ModelMap mmap)
    {
        return prefix + "/add";
    }

    /**
     * 新增保存基站管理
     */
    @RequiresPermissions("system:locator:add")
    @Log(title = "基站管理", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(SysLocator sysLocator)
    {
        return toAjax(sysLocatorService.insertSysLocator(sysLocator));
    }

    /**
     * 修改基站管理
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        SysLocator sysLocator = sysLocatorService.selectSysLocatorById(id);
        mmap.put("sysLocator", sysLocator);
        return prefix + "/edit";
    }
    /**
     * 配置基站坐标
     */
    @GetMapping("/locatorCoordinate/{id}")
    public String locatorCoordinate(@PathVariable("id") Long id, ModelMap mmap)
    {
        SysLocator sysLocator = sysLocatorService.selectSysLocatorById(id);
        mmap.put("sysLocator", sysLocator);
        //获取地图配置数据
        List<SysEsmap> sysEsmapList = sysEsmapService.selectSysEsmapList(new SysEsmap());
        if (!StringUtils.isEmpty(sysEsmapList)){
            mmap.put("sysEsmap", sysEsmapList.get(0));
        }
        return prefix + "/locatorCoordinate";
    }
    /**
     * 修改保存基站管理
     */
    @RequiresPermissions("system:locator:edit")
    @Log(title = "基站管理", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(SysLocator sysLocator)
    {
        return toAjax(sysLocatorService.updateSysLocator(sysLocator));
    }
    /**
     * 修改保存基站管理
     */
    @RequiresPermissions("system:locator:edit")
    @Log(title = "基站管理", businessType = BusinessType.UPDATE)
    @PostMapping("/updateCoordinate")
    @ResponseBody
    public AjaxResult updateCoordinate(SysLocator sysLocator)
    {
        return toAjax(sysLocatorService.updateSysLocator(sysLocator));
    }
    /**
     * 删除基站管理
     */
    @RequiresPermissions("system:locator:remove")
    @Log(title = "基站管理", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(sysLocatorService.deleteSysLocatorByIds(ids));
    }

    /**
     * 分配用户
     */
    @RequiresPermissions("system:locator:edit")
    @GetMapping("/authTag/{locatorId}")
    public String authUser(@PathVariable("locatorId") Long locatorId, ModelMap mmap)
    {
        mmap.put("locator", sysLocatorService.selectSysLocatorById(locatorId));
        return prefix + "/authTag";
    }
    /**
     * 查询已分配用户角色列表
     */
    @RequiresPermissions("system:locator:list")
    @PostMapping("/authTag/allocatedList")
    @ResponseBody
    public TableDataInfo allocatedList(Tag tag)
    {
        startPage();
        List<Tag> list = sysLocatorService.selectAllocatedList(tag);
        return getDataTable(list);
    }
    /**
     * 取消授权
     */
    @Log(title = "基站管理", businessType = BusinessType.GRANT)
    @PostMapping("/authTag/cancel")
    @ResponseBody
    public AjaxResult cancelAuthTag(TagLocator tagLocator)
    {
        return toAjax(sysLocatorService.deleteAuthTag(tagLocator));
    }
    /**
     * 批量取消授权
     */
    @Log(title = "基站管理", businessType = BusinessType.GRANT)
    @PostMapping("/authTag/cancelAll")
    @ResponseBody
    public AjaxResult cancelAuthTagAll(Long id, String tagIds)
    {
        SysLocator sysLocator = sysLocatorService.selectSysLocatorById(id);
        String locatorId = sysLocator.getLocatorId();
        return toAjax(sysLocatorService.deleteAuthTags(locatorId, tagIds));
    }
    /**
     * 选择标签
     */
    @GetMapping("/authTag/selectTag/{locatorId}")
    public String selectUser(@PathVariable("locatorId") Long locatorId, ModelMap mmap)
    {
        mmap.put("locator", sysLocatorService.selectSysLocatorById(locatorId));
        return prefix + "/selectTag";
    }
    /**
     * 查询未分配用户角色列表
     */
    @RequiresPermissions("system:locator:list")
    @PostMapping("/authTag/unallocatedList")
    @ResponseBody
    public TableDataInfo unallocatedList(Tag tag)
    {
        startPage();
        List<Tag> list = lableService.selectUnallocatedList(tag);
        return getDataTable(list);
    }
    /**
     * 批量选择用户授权
     */
    @Log(title = "基站管理", businessType = BusinessType.GRANT)
    @PostMapping("/authTag/selectAll")
    @ResponseBody
    public AjaxResult selectAuthUserAll(String locatorId, String tagIds)
    {
        return AjaxResult.success(sysLocatorService.insertAuthTags(locatorId, tagIds));
    }

    @PostMapping("/getLocatorMacList")
    @ResponseBody
    public AjaxResult getLocatorMacList(){
        String locatorMacs = StaticRedisUtil.staticRedisUtil.get("locatorMacs")==null?"":StaticRedisUtil.staticRedisUtil.get("locatorMacs").toString();
        List<LocatorMacCtx> list = new ArrayList<>();
        List<String> macBiglist = new ArrayList<>();
        if (!StringUtils.isEmpty(locatorMacs)){
            list = JSONObject.parseArray(locatorMacs, LocatorMacCtx.class);
            for (LocatorMacCtx l:list){
                String macLe = l.getMac();
                String macBig = "";
                //小端转大端
                List<String> macLelist = new ArrayList<>();
                for (int i = 0 ; i < macLe.length();i=i+2){
                    macLelist.add(macLe.substring(i,i+2));
                }
                Collections.reverse(macLelist);
                for (String str:macLelist){
                    macBig = macBig+str;
                }
                macBiglist.add(macBig);
            }
        }
        return AjaxResult.success(macBiglist);
    }
    @PostMapping("/authTag/rpcDown/{id}")
    @ResponseBody
    public AjaxResult rpcDown(@PathVariable("id") Long id){
        SysLocator sysLocator = sysLocatorService.selectSysLocatorById(id);
        String locatorId = sysLocator.getLocatorId();
        //查询基站下所有的标签
        Tag tag = new Tag();
        tag.setLocatorId(locatorId);
        List<Tag> list = sysLocatorService.selectAllocatedList(tag);
        List<String> macList = new ArrayList<>();
        String macs = "";
        if (!list.isEmpty()){
            for (Tag t:list){
                String macBIg = t.getTagId();
                //大端转小端
                List<String> macLelist = new ArrayList<>();
                for (int i = 0 ; i < macBIg.length();i=i+2){
                    macLelist.add(macBIg.substring(i,i+2));
                }
                Collections.reverse(macLelist);
                for (String str:macLelist){
                    macs = macs+str;
                }
            }
        }
        //执行下发操作  macs

        return AjaxResult.success();
    }
}
